package com.itheima.app.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLikeManager {

    @Autowired
    private HuanXinTemplate huanXinTemplate;


    @Reference
    private UserLikeService userLikeService;

    @Reference
    private FriendService friendService;

    @Reference
    private UserInfoService userInfoService;

    //喜欢
    public void love(Long likeUserId) {
        //获取登录id
        Long userId = UserHolder.getUser().getId();

        //向mongo中添加喜欢记录
        userLikeService.save(userId, likeUserId);

        //查询一下二人是否相互喜欢
        boolean flag = userLikeService.isMutualLike(userId, likeUserId);

        //判断是否相互喜欢
        if (flag) {//是
            //1.向mongo中的friend表中记录好友关系
            friendService.addContacts(userId, likeUserId);

            //2.向环信中记录好友
            huanXinTemplate.addContacts(userId, likeUserId);
        }
    }

    //不喜欢
    public void unlove(Long likeUserId) {
        //获取用户id
        Long userId = UserHolder.getUser().getId();

        //向mongo中删除喜欢记录(user_like)
        userLikeService.delete(userId, likeUserId);

        //删除mongo中的friend表中好友关系(friend)
        friendService.deleteContacts(userId, likeUserId);

        //删除环信中好友记录
        huanXinTemplate.deleteContacts(userId, likeUserId);
    }


    //喜欢统计
    public UserLikeCountVo counts() {
        //获取登录用户id
        Long userId = UserHolder.getUser().getId();
        //调用service直接查询
        return userLikeService.findUserLikeCount(userId);
    }


    //喜欢详情
    public PageBeanVo findType(Integer pageNum, Integer pageSize, Integer type) {
        //1.获取用户id
        Long userId = UserHolder.getUser().getId();
        // 2、声明返回结果
        PageBeanVo pageBeanVo = null;

        // 3、类型判断
        switch (type) {
            case 1: { // 好友
                pageBeanVo = userLikeService.findEachLove(pageNum, pageSize, userId);
                break;
            }
            case 2: { // 我喜欢
                pageBeanVo = userLikeService.findLove(pageNum, pageSize, userId);
                break;
            }
            case 3: { // 粉丝
                pageBeanVo = userLikeService.findFan(pageNum, pageSize, userId);
                break;
            }
            case 4: { // 谁看过我
                pageBeanVo = userLikeService.findVisitor(pageNum, pageSize, userId);
                break;
            }
        }
        // 4、封装 vo
        List<RecommendUser> recommendUserList = (List<RecommendUser>) pageBeanVo.getItems();
        List<UserLikeVo> userLikeVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(recommendUserList)) {
            for (RecommendUser recommendUser : recommendUserList) {
                UserLikeVo userLikeVo = new UserLikeVo();
                UserInfo userInfo = userInfoService.findById(recommendUser.getUserId());
                BeanUtil.copyProperties(userInfo, userLikeVo);//复制用户信息部分
                userLikeVo.setMatchRate(recommendUser.getScore().intValue()); // 缘分值

                //粉丝
                if (type == 3) {
                    //判断二人是否相互喜欢
                    if (userLikeService.isMutualLike(recommendUser.getUserId(), recommendUser.getToUserId())) {
                        userLikeVo.setAlreadyLove(true);//设置为喜欢他
                    }
                }
                userLikeVoList.add(userLikeVo);
              }
            }
            pageBeanVo.setItems(userLikeVoList);

            return pageBeanVo;
        }
}
